<template>
    <AdminSkeleton :headerIsHeight="true">
        <template #header>
            <div class="params-form">
                <el-form label-width="auto" :model="paramsForm">
                    <div class="one-box">
                        <el-form-item label="类型:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['',undefined, null]">
                                <el-select v-model="paramsForm.lb_type" 
                                    multiple
                                    clearable
                                    collapse-tags
                                    collapse-tags-tooltip
                                    placeholder="可留空" size="small" style="width: 180px"
                                    @change="changeLbType"  
                                    @visible-change="getLbType"                    
                                >
                                <template #header>
                                        <el-checkbox
                                            v-model="lbTypeCheckAll"
                                            :indeterminate="lbTypeIndeterminate"
                                            @change="lbTypeHandleCheckAll"
                                        >
                                        全选
                                        </el-checkbox>
                                    </template>
                                    <el-option v-for="item in lbTypeArr" :label="item.DESCRIPTION" :value="item.VALUE" :key="item.VALUE"/>
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item label="性别:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['',undefined, null]">
                                <el-select v-model="paramsForm.lb_sex" 
                                    multiple
                                    clearable
                                    collapse-tags
                                    collapse-tags-tooltip
                                    placeholder="可留空" size="small" style="width: 120px"
                                    @change="changeLbSex"  
                                    @visible-change="getLbSex"                    
                                >
                                <template #header>
                                        <el-checkbox
                                            v-model="lbSexCheckAll"
                                            :indeterminate="lbSexIndeterminate"
                                            @change="lbSexHandleCheckAll"
                                        >
                                        全选
                                        </el-checkbox>
                                    </template>
                                    <el-option v-for="item in lbSexArr" :label="item.DESCRIPTION" :value="item.VALUE" :key="item.VALUE"/>
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item label="身高:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['',undefined, null]">
                                <el-select v-model="paramsForm.lb_height" 
                                    multiple
                                    clearable
                                    collapse-tags
                                    collapse-tags-tooltip
                                    placeholder="可留空" size="small" style="width: 150px"
                                    @change="changeLbHeight"  
                                    @visible-change="getLbHeight"                    
                                >
                                <template #header>
                                        <el-checkbox
                                            v-model="lbHeightCheckAll"
                                            :indeterminate="lbHeightIndeterminate"
                                            @change="lbHeightHandleCheckAll"
                                        >
                                        全选
                                        </el-checkbox>
                                    </template>
                                    <el-option v-for="item in lbHeightArr" :label="item.DESCRIPTION" :value="item.VALUE" :key="item.VALUE"/>
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" size="small" @click="getDatas(paramsForm)" :loading="loading">查询</el-button>
                        </el-form-item>
                    </div>
                    <div class="two-box">
                        <el-form-item label="胸围:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['',undefined, null]">
                                <el-select v-model="paramsForm.lb_bust" 
                                    multiple
                                    clearable
                                    collapse-tags
                                    collapse-tags-tooltip
                                    placeholder="可留空" size="small" style="width: 180px"
                                    @change="changeLbBust"  
                                    @visible-change="getLbBust"                    
                                >
                                <template #header>
                                        <el-checkbox
                                            v-model="lbBustCheckAll"
                                            :indeterminate="lbBustIndeterminate"
                                            @change="lbBustHandleCheckAll"
                                        >
                                        全选
                                        </el-checkbox>
                                    </template>
                                    <el-option v-for="item in lbBustArr" :label="item.DESCRIPTION" :value="item.VALUE" :key="item.VALUE"/>
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item label="尺码:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['',undefined, null]">
                                <el-select v-model="paramsForm.lb_size" 
                                    multiple
                                    clearable
                                    collapse-tags
                                    collapse-tags-tooltip
                                    placeholder="可留空" size="small" style="width: 180px"
                                    @change="changeLbSize"  
                                    @visible-change="getLbSize"                    
                                >
                                <template #header>
                                        <el-checkbox
                                            v-model="lbSizeCheckAll"
                                            :indeterminate="lbSizeIndeterminate"
                                            @change="lbSizeHandleCheckAll"
                                        >
                                        全选
                                        </el-checkbox>
                                    </template>
                                    <el-option v-for="item in lbSizeArr" :label="item.DESCRIPTION" :value="item.VALUE" :key="item.VALUE"/>
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item label="版型:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['',undefined, null]">
                                <el-select v-model="paramsForm.lb_stereotype" 
                                    multiple
                                    clearable
                                    collapse-tags
                                    collapse-tags-tooltip
                                    placeholder="可留空" size="small" style="width: 180px"
                                    @change="changeLbStreroType"  
                                    @visible-change="getLbStreroType"                    
                                >
                                <template #header>
                                        <el-checkbox
                                            v-model="lbStreroTypeCheckAll"
                                            :indeterminate="lbStreroTypeIndeterminate"
                                            @change="lbStreroTypeHandleCheckAll"
                                        >
                                        全选
                                        </el-checkbox>
                                    </template>
                                    <el-option v-for="item in lbStreroTypeArr" :label="item.DESCRIPTION" :value="item.VALUE" :key="item.VALUE"/>
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                    </div>
                </el-form>
            </div>
        </template>
        <template #control>
            <Control :batchIsShow="batchIsShow" :downloadBoxShow="downloadBoxShow" :permObj="permObj"
                @addClick="editDatas('add', {})" @showDownload="showDownloadBox"
                @downloadExcelPaging="downloadExcelPaging(newDatasArr, 'contentTable', '标签值信息')"
                @downloadExcelAll="downloadExcelAll(datasArr, '标签值信息')" @batchToggleShow="batchEdit"
                @batchDel="editSubmit('batchDel', selectRows)" @batchUpload="batchUpload"></Control>
            <Paging :dataLength="datasLength" :countPage="countPage" :btnDisabledObj="btnDisabledObj"
                v-model:firstPage="firstPage" v-model:pageSize="pageSize" @toNext="nextPage(firstPage)"
                @toLast="lastPage(firstPage)" @toFirst="toFirstPage" @toEnd="toEndPage" @toSpecify="toSpecifyPage"
                @changePsize="changePageSize"></Paging>
        </template>
        <template #content>
            <el-table :data="newDatasArr" style="width: 100%" v-loading="loading"
                @selection-change="handleSelectionChange" id="contentTable">
                <el-table-column type="selection" width="50" v-if="selectShow" />
                <el-table-column prop="LBNUM" label="编码" width="150" />
                <el-table-column prop="DESCRIPTION" label="名称" width="250" />
                <el-table-column prop="QUNIT" label="单位" width="70" />
                <el-table-column prop="LB_TYPE" label="类型" width="80" />
                <el-table-column prop="LB_SEX" label="性别" width="70" />
                <el-table-column prop="LB_HEIGHT" label="身高" width="70" />
                <el-table-column prop="LB_BUST" label="胸围" width="70" />
                <el-table-column prop="LB_SIZE" label="尺码" width="80" />
                <el-table-column prop="LB_STEREOTYPE" label="特征" width="150" />
                <el-table-column fixed="right" label="Operations" min-width="180"
                    v-perm="permObj.update"
                >
                    <template #default="{row}">
                        <el-button type="primary" size="small" 
                            @click="editDatas('update',row)" title="编辑"
                            v-perm="permObj.update"
                        >
                            <el-icon><Edit /></el-icon>
                        </el-button>
                        <el-button type="warning" size="small" 
                            @click="editDatas('copy',row)" title="复制"
                            v-perm="permObj.add"
                        >
                            <el-icon><CopyDocument /></el-icon>
                        </el-button>
                        <el-button  type="danger" size="small" class="del" title="删除" 
                            @click="editSubmit('delete',row)"
                            v-perm="permObj.delete"
                        >
                            <el-icon><DeleteFilled /></el-icon>
                        </el-button>
                    </template>
                </el-table-column>  
            </el-table>
        </template>
    </AdminSkeleton>
    <Drawer ref="drawerRef" :title="title" :size="size" @submit="editSubmit(submitType, editForm)"
        @cancel-submit="resetForm">
        <el-form :model="editForm"  
            ref="updateFormRef"
            :rules="rules"
        >
            <el-form-item label="物资编码" :label-width="formLabelWidth" prop="LBNUM">
                <el-input v-model="editForm.LBNUM" autocomplete="off"/>
            </el-form-item>
            <el-form-item label="名称" :label-width="formLabelWidth" prop="DESCRIPTION">
                <el-input v-model="editForm.DESCRIPTION" autocomplete="off" />
            </el-form-item>
            <el-form-item label="单位" :label-width="formLabelWidth" prop="QUNIT">
                <el-input v-model="editForm.QUNIT" autocomplete="off" />
            </el-form-item>
            <el-form-item label="类型" :label-width="formLabelWidth" prop="LB_TYPE">
                <el-input v-model="editForm.LB_TYPE" autocomplete="off" />
            </el-form-item>
            <el-form-item label="性别" :label-width="formLabelWidth" prop="LB_SEX">
                <el-input v-model="editForm.LB_SEX" autocomplete="off" />
            </el-form-item>
            <el-form-item label="身高" :label-width="formLabelWidth" prop="LB_HEIGHT">
                <el-input v-model="editForm.LB_HEIGHT" autocomplete="off" />
            </el-form-item>
            <el-form-item label="胸围" :label-width="formLabelWidth" prop="LB_BUST">
                <el-input v-model="editForm.LB_BUST" autocomplete="off" />
            </el-form-item>
            <el-form-item label="尺码" :label-width="formLabelWidth" prop="LB_SIZE">
                <el-input v-model="editForm.LB_SIZE" autocomplete="off" />
            </el-form-item>
            <el-form-item label="特征" :label-width="formLabelWidth" prop="LB_STEREOTYPE">
                <el-input v-model="editForm.LB_STEREOTYPE" autocomplete="off" />
            </el-form-item>
      </el-form>
    </Drawer>
    <Dialog ref="dialogRef" :uploadKeys="uploadKeys" @uploadFile="uploadFile" @closeDialog="closeDialog"
        @batchUpdate="editSubmit('batchUpdate', uploadTable)" @batchAdd="editSubmit('batchAdd', uploadTable)"
        @downloadTemplate="downloadTemplate('upload-table', '标签值信息导入模板')">
        <el-table :data="uploadTable" style="width: 100%" ref="dialogTableRef" id="upload-table">
            <!-- <el-table-column type="selection" width="50" v-if="selectShow"/> -->
            <el-table-column fixed prop="LBNUM" label="物资编码" width="150" />
            <el-table-column prop="DESCRIPTION" label="名称" width="150" />
            <el-table-column prop="QUNIT" label="单位" width="120" />
            <el-table-column prop="LB_TYPE" label="类型" width="150" />
            <el-table-column prop="LB_SEX" label="性别" width="150" />
            <el-table-column prop="LB_HEIGHT" label="身高" width="150" />
            <el-table-column prop="LB_BUST" label="胸围" width="150" />
            <el-table-column prop="LB_SIZE" label="尺码" width="150" />
            <el-table-column prop="LB_STEREOTYPE" label="特征" width="150" /> 
        </el-table>
    </Dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
import AdminSkeleton from '@/components/AdminSkeleton.vue';
import Paging from '@/components/Paging.vue'
import Control from '@/components/Control.vue'
import Dialog from '@/components/Dialog.vue'
import Drawer from '@/components/Drawer.vue'
import { getLbListInfo, editLbListInfo, editManyLbListInfo } from '@/utils/lpsApi/lb_list'
import { getLbValuesInfo } from '@/utils/lpsApi/lb_values'
import { paging } from '@/utils/paging'
import { exportClickFromTableId, exportClickFromDatas } from '@/utils/downloadExcel'
import { objCopy } from '@/utils/objCopy'
import { msgOpen } from '@/utils/msg'
import { confirm, confirmBatch } from '@/utils/confirm'
import { lpsStore } from '@/stores/lpsStore';

const useLpsStore = lpsStore()
// console.log(useLpsStore)

//权限对象--用于控制按钮显示
const componentName = 'Lb_list'
const permObj = {
    add: { name: componentName, type: 'add' },
    update: { name: componentName, type: 'update' },
    delete: { name: componentName, type: 'delete' }
}


//#region 查询***********************************************************************************************************************
const {
    datasArr,
    datasLength,
    firstPage,
    pageSize,
    countPage,
    newDatasArr,
    btnDisabledObj,
    pagingPreprocessing,
    nextPage,
    lastPage,
    toFirstPage,
    toEndPage,
    toSpecifyPage,
    changePageSize
} = paging() //分页


//查询参数表单对象
const paramsForm = reactive({
    lb_type:[],
    lb_sex:[],
    lb_height:[],
    lb_bust:[],
    lb_size:[],
    lb_stereotype:[]
})

/**
 * 获取参数函数封装
 * @param store 存储对象
 * @param attribute 存储对象的属性
 * @param method 设置存储对象属性数据的函数
 * @param getFun 获取参数数据的函数
 * @param colName 字段名
 * @param colVal 字段值
 */
const getParamFunc = async (store,attribute,method,getFun,colName,colVal)=>{  
    if(!store[attribute].length){
        let res = await getFun()
        if(res.status){          
            store[method](res.resDatas.result)
            return store[attribute].filter(item=>item[colName] === colVal)
        }
    }else{
        return store[attribute].filter(item=>item[colName] === colVal)
    } 
}


/**
 * 参数改变函数封装
 * 改变选择的参数
 * @param checkedArr 选中的值
 * @param paransArr 参数数组
 * @param isAll 全选变量
 * @param isInd 半选变量
 */
const changeParamFun = (checkedArr,paransArr,isAll,isInd)=>{
    if(Array.isArray(checkedArr)){
        if(checkedArr.length === 1){
            //取消全选
            if(paransArr.value.length && checkedArr.length !== paransArr.value.length) {
                isAll.value = false
                isInd.value = true
            } 
        }
        else if(checkedArr.length === 0){
            isAll.value = false
            isInd.value = false
        }
        else if(checkedArr.length > 1){
            // 当val长度大于1切等于站点数组长度时，全选勾选
            if(checkedArr.length === paransArr.value.length){
                isAll.value = true
                isInd.value = false  
            } else{
                isAll.value = false
                isInd.value = true
            }
        }
    }
}

/**
 * 
 * @param isAllCheck 全选状态
 * @param isInd 是否半选
 * @param paramArr 参数值数组对象
 */
const paramHandleCheckAll = (isAllCheck,isInd,paramArr)=>{
    isInd.value = false
    return isAllCheck ? paramArr.value.map((_) => _.VALUE) : []
}


//参数1-类型
let lbTypeArr = ref([])
let lbTypeCheckAll = ref(false)
let lbTypeIndeterminate = ref(false)

//获取类型
const getLbType = async () => lbTypeArr.value = await getParamFunc(useLpsStore,"lbValInfo","setLbValInfo",getLbValuesInfo,"CUST_TYPE","LB_TYPE")
// const getLbType = async ()=>{
//     //获取数据
//     if(!useLpsStore.lbValInfo.length){
//         let res = await getLbValuesInfo()
//         if(res.status){          
//             useLpsStore.setLbValInfo(res.resDatas.result)
//             lbTypeArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_TYPE")
//         }
//     }else{
//          lbTypeArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_TYPE")
//     } 
// }

/**
 * 改变选择的类型
 * @param val 选中的值
 */
 const changeLbType = (val) => changeParamFun(val,lbTypeArr,lbTypeCheckAll,lbTypeIndeterminate)
// const changeLbType = (val)=>{
//     if(Array.isArray(val)){
//         if(val.length === 1){
//             //取消全选
//             if(lbTypeArr.value.length && val.length !== lbTypeArr.value.length) {
//                 lbTypeCheckAll.value = false
//                 lbTypeIndeterminate.value = true
//             } 
//         }
//         else if(val.length === 0){
//             lbTypeCheckAll.value = false
//             lbTypeIndeterminate.value = false
//         }
//         else if(val.length > 1){
//             // 当val长度大于1切等于站点数组长度时，全选勾选
//             if(val.length === lbTypeArr.value.length){
//                 lbTypeCheckAll.value = true
//                 lbTypeIndeterminate.value = false  
//             } else{
//                 lbTypeCheckAll.value = false
//                 lbTypeIndeterminate.value = true
//             }
//         }
//     }
// }

/**
 * 类型全选
 * @param val 是否全选
 */
 const lbTypeHandleCheckAll = (val) => paramsForm.lb_type = paramHandleCheckAll(val,lbTypeIndeterminate,lbTypeArr)
// const lbTypeHandleCheckAll = (val)=>{
//     lbTypeIndeterminate.value = false
//     if (val) {
//         paramsForm.lb_type = lbTypeArr.value.map((_) => _.VALUE)
//     } else {
//         paramsForm.lb_type = []
//     }
// }
/*****************************************************************/

//参数2-性别
let lbSexArr = ref([])
let lbSexCheckAll = ref(false)
let lbSexIndeterminate = ref(false)

//获取性别
const getLbSex = async () => lbSexArr.value = await getParamFunc(useLpsStore,"lbValInfo","setLbValInfo",getLbValuesInfo,"CUST_TYPE","LB_SEX")
// const getLbSex = async ()=>{
//     // console.log(useLpsStore.lbValInfo)
//     //获取数据
//     if(!useLpsStore.lbValInfo.length){
//         let res = await getLbValuesInfo()
//         if(res.status){          
//             useLpsStore.setLbValInfo(res.resDatas.result)
//             lbSexArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_SEX")
//         }
//     }else{
//          lbSexArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_SEX")
//     } 
//     // console.log(lbSexArr.value)
// }

/**
 * 改变选择的性别
 * @param val 选中的值
 */
const changeLbSex = (val) => changeParamFun(val,lbSexArr,lbSexCheckAll,lbSexIndeterminate)
// const changeLbSex = (val)=>{
//     if(Array.isArray(val)){
//         if(val.length === 1){
//             //取消全选
//             if(lbSexArr.value.length && val.length !== lbSexArr.value.length) {
//                 lbSexCheckAll.value = false
//                 lbSexIndeterminate.value = true
//             } 
//         }
//         else if(val.length === 0){
//             lbSexCheckAll.value = false
//             lbSexIndeterminate.value = false
//         }
//         else if(val.length > 1){
//             // 当val长度大于1切等于站点数组长度时，全选勾选
//             if(val.length === lbSexArr.value.length){
//                 lbSexCheckAll.value = true
//                 lbSexIndeterminate.value = false  
//             } else{
//                 lbSexCheckAll.value = false
//                 lbSexIndeterminate.value = true
//             }
//         }
//     }
// }


/**
 * 性别全选
 * @param val 是否全选
 */
 const lbSexHandleCheckAll = (val) => paramsForm.lb_sex = paramHandleCheckAll(val,lbSexIndeterminate,lbSexArr)
// const lbSexHandleCheckAll = (val)=>{
//     lbSexIndeterminate.value = false
//     if (val) {
//         paramsForm.lb_sex = lbSexArr.value.map((_) => _.VALUE)
//     } else {
//         paramsForm.lb_sex = []
//     }
// }

/************************************************************************************** */


//参数3-身高
let lbHeightArr = ref([])
let lbHeightCheckAll = ref(false)
let lbHeightIndeterminate = ref(false)

//获取身高
const getLbHeight = async () => lbHeightArr.value = await getParamFunc(useLpsStore,"lbValInfo","setLbValInfo",getLbValuesInfo,"CUST_TYPE","LB_HEIGTH")
// const getLbHeight = async ()=>{
//     // console.log(useLpsStore.lbValInfo)
//     //获取数据
//     if(!useLpsStore.lbValInfo.length){
//         let res = await getLbValuesInfo()
//         if(res.status){          
//             useLpsStore.setLbValInfo(res.resDatas.result)
//             lbHeightArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_HEIGTH")
//         }
//     }else{
//          lbHeightArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_HEIGTH")
//     } 
//     // console.log(lbHeightArr.value)
// }
/**
 * 改变选择的身高
 * @param val 选中的值
 */
const changeLbHeight = (val) => changeParamFun(val,lbHeightArr,lbHeightCheckAll,lbHeightIndeterminate)
// const changeLbHeight = (val)=>{
//     if(Array.isArray(val)){
//         if(val.length === 1){
//             //取消全选
//             if(lbHeightArr.value.length && val.length !== lbHeightArr.value.length) {
//                 lbHeightCheckAll.value = false
//                 lbHeightIndeterminate.value = true
//             } 
//         }
//         else if(val.length === 0){
//             lbHeightCheckAll.value = false
//             lbHeightIndeterminate.value = false
//         }
//         else if(val.length > 1){
//             // 当val长度大于1切等于站点数组长度时，全选勾选
//             if(val.length === lbHeightArr.value.length){
//                 lbHeightCheckAll.value = true
//                 lbHeightIndeterminate.value = false  
//             } else{
//                 lbHeightCheckAll.value = false
//                 lbHeightIndeterminate.value = true
//             }
//         }
//     }
// }

/**
 * 身高全选
 * @param val 是否全选
 */
const lbHeightHandleCheckAll = (val) => paramsForm.lb_height = paramHandleCheckAll(val,lbHeightIndeterminate,lbHeightArr)
// const lbHeightHandleCheckAll = (val)=>{
//     lbHeightIndeterminate.value = false
//     if (val) {
//         paramsForm.lb_height = lbHeightArr.value.map((_) => _.VALUE)
//     } else {
//         paramsForm.lb_height = []
//     }
// }
/************************************************************************************** */


//参数4-胸围
let lbBustArr = ref([])
let lbBustCheckAll = ref(false)
let lbBustIndeterminate = ref(false)

//获取胸围
const getLbBust = async () => lbBustArr.value = await getParamFunc(useLpsStore,"lbValInfo","setLbValInfo",getLbValuesInfo,"CUST_TYPE","LB_BUST")
// const getLbBust = async ()=>{
//     // console.log(useLpsStore.lbValInfo)
//     //获取数据
//     if(!useLpsStore.lbValInfo.length){
//         let res = await getLbValuesInfo()
//         if(res.status){          
//             useLpsStore.setLbValInfo(res.resDatas.result)
//             lbBustArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_BUST")
//         }
//     }else{
//          lbBustArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_BUST")
//     } 
//     // console.log(lbBustArr.value)
// }

/**
 * 改变选择的胸围
 * @param val 选中的值
 */
const changeLbBust = (val) => changeParamFun(val,lbBustArr,lbBustCheckAll,lbBustIndeterminate)
// const changeLbBust = (val)=>{
//     if(Array.isArray(val)){
//         if(val.length === 1){
//             //取消全选
//             if(lbBustArr.value.length && val.length !== lbBustArr.value.length) {
//                 lbBustCheckAll.value = false
//                 lbBustIndeterminate.value = true
//             } 
//         }
//         else if(val.length === 0){
//             lbBustCheckAll.value = false
//             lbBustIndeterminate.value = false
//         }
//         else if(val.length > 1){
//             // 当val长度大于1切等于站点数组长度时，全选勾选
//             if(val.length === lbBustArr.value.length){
//                 lbBustCheckAll.value = true
//                 lbBustIndeterminate.value = false  
//             } else{
//                 lbBustCheckAll.value = false
//                 lbBustIndeterminate.value = true
//             }
//         }
//     }
// }

/**
 * 胸围全选
 * @param val 是否全选
 */
const lbBustHandleCheckAll = (val) => paramsForm.lb_bust = paramHandleCheckAll(val,lbBustIndeterminate,lbBustArr)
// const lbBustHandleCheckAll = (val)=>{
//     lbBustIndeterminate.value = false
//     if (val) {
//         paramsForm.lb_bust = lbBustArr.value.map((_) => _.VALUE)
//     } else {
//         paramsForm.lb_bust = []
//     }
// }
/************************************************************************************** */

//参数5-尺码
let lbSizeArr = ref([])
let lbSizeCheckAll = ref(false)
let lbSizeIndeterminate = ref(false)

//获取尺码
const getLbSize = async () => lbSizeArr.value = await getParamFunc(useLpsStore,"lbValInfo","setLbValInfo",getLbValuesInfo,"CUST_TYPE","LB_SIZE")
// const getLbSize = async ()=>{
//     // console.log(useLpsStore.lbValInfo)
//     //获取数据
//     if(!useLpsStore.lbValInfo.length){
//         let res = await getLbValuesInfo()
//         if(res.status){          
//             useLpsStore.setLbValInfo(res.resDatas.result)
//             lbSizeArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_SIZE")
//         }
//     }else{
//          lbSizeArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_SIZE")
//     } 
//     // console.log(lbSizeArr.value)
// }

/**
 * 改变选择的尺码
 * @param val 选中的值
 */
const changeLbSize = (val) => changeParamFun(val,lbSizeArr,lbSizeCheckAll,lbSizeIndeterminate)
// const changeLbSize = (val)=>{
//     if(Array.isArray(val)){
//         if(val.length === 1){
//             //取消全选
//             if(lbSizeArr.value.length && val.length !== lbSizeArr.value.length) {
//                 lbSizeCheckAll.value = false
//                 lbSizeIndeterminate.value = true
//             } 
//         }
//         else if(val.length === 0){
//             lbSizeCheckAll.value = false
//             lbSizeIndeterminate.value = false
//         }
//         else if(val.length > 1){
//             // 当val长度大于1切等于站点数组长度时，全选勾选
//             if(val.length === lbSizeArr.value.length){
//                 lbSizeCheckAll.value = true
//                 lbSizeIndeterminate.value = false  
//             } else{
//                 lbSizeCheckAll.value = false
//                 lbSizeIndeterminate.value = true    
//             }
//         }
//     }
// }

/**
 * 尺码全选
 * @param val 是否全选
 */
const lbSizeHandleCheckAll = (val) => paramsForm.lb_size = paramHandleCheckAll(val,lbSizeIndeterminate,lbSizeArr)
// const lbSizeHandleCheckAll = (val)=>{
//     lbSizeIndeterminate.value = false
//     if (val) {
//         paramsForm.lb_size = lbSizeArr.value.map((_) => _.VALUE)
//     } else {
//         paramsForm.lb_size = []
//     }
// }

/************************************************************************************** */

//参数6-版型
let lbStreroTypeArr = ref([])
let lbStreroTypeCheckAll = ref(false)
let lbStreroTypeIndeterminate = ref(false)  

//获取版型
const getLbStreroType = async () => lbStreroTypeArr.value = await getParamFunc(useLpsStore,"lbValInfo","setLbValInfo",getLbValuesInfo,"CUST_TYPE","LB_STEREOTYPE")
// const getLbStreroType = async ()=>{
//     // console.log(useLpsStore.lbValInfo)
//     //获取数据
//     if(!useLpsStore.lbValInfo.length){
//         let res = await getLbValuesInfo()
//         if(res.status){          
//             useLpsStore.setLbValInfo(res.resDatas.result)
//             lbStreroTypeArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_STEREOTYPE")
//         }
//     }else{
//          lbStreroTypeArr.value = useLpsStore.lbValInfo.filter(item=>item.CUST_TYPE === "LB_STEREOTYPE")
//     } 
//     // console.log(lbStreroTypeArr.value)
// }

/**
 * 改变选择的版型
 * @param val 选中的值
 */
const changeLbStreroType = (val) => changeParamFun(val,lbStreroTypeArr,lbStreroTypeCheckAll,lbStreroTypeIndeterminate)
// const changeLbStreroType = (val)=>{
//     if(Array.isArray(val)){
//         if(val.length === 1){
//             //取消全选
//             if(lbStreroTypeArr.value.length && val.length !== lbStreroTypeArr.value.length) {
//                 lbStreroTypeCheckAll.value = false
//                 lbStreroTypeIndeterminate.value = true
//             } 
//         }
//         else if(val.length === 0){
//             lbStreroTypeCheckAll.value = false
//             lbStreroTypeIndeterminate.value = false
//         }
//         else if(val.length > 1){
//             // 当val长度大于1切等于站点数组长度时，全选勾选
//             if(val.length === lbStreroTypeArr.value.length){
//                 lbStreroTypeCheckAll.value = true
//                 lbStreroTypeIndeterminate.value = false  
//             } else{
//                 lbStreroTypeCheckAll.value = false
//                 lbStreroTypeIndeterminate.value = true    
//             }
//         }
//     }
// }

/**
 * 版型全选
 * @param val 是否全选
 */
const lbStreroTypeHandleCheckAll = (val) => paramsForm.lb_stereotype = paramHandleCheckAll(val,lbStreroTypeIndeterminate,lbStreroTypeArr)
// const lbStreroTypeHandleCheckAll = (val)=>{
//     lbStreroTypeIndeterminate.value = false
//     if (val) {
//         paramsForm.lb_stereotype = lbStreroTypeArr.value.map((_) => _.VALUE)
//     } else {
//         paramsForm.lb_stereotype = []
//     }
// }
/************************************************************************************** */


// loading状态
let loading = ref(false);

//获取标签值列表
const getDatas = async (params) => {
    console.log(params)
    loading.value = true;
    const res = await getLbListInfo(params)
    pagingPreprocessing(res.resDatas.result)
    loading.value = false;
}
//#endregion********************************************************************************************************




//#region 编辑信息***************************************************************************************************
let title = ref("")
let size = ref("40%")
let formLabelWidth = ref("110")

//编辑用户信息
let drawerRef = ref(null) //组件ref
let oldLbValue = reactive({
    LBNUM:'',
    DESCRIPTION:'',
    QUNIT:'',
    LB_TYPE:'',
    LB_SEX:'',
    LB_HEIGHT:'',
    LB_BUST:'',
    LB_SIZE:'',
    LB_STEREOTYPE:''
})
let editForm = reactive({
    LBNUM:'',
    DESCRIPTION:'',
    QUNIT:'',
    LB_TYPE:'',
    LB_SEX:'',
    LB_HEIGHT:'',
    LB_BUST:'',
    LB_SIZE:'',
    LB_STEREOTYPE:''
})
//提交类型
let submitType = ref('')
//表单验证ref
let updateFormRef = ref(null)
//输入框验证规则
let rules = reactive({
    LBNUM: [
        { required: true, message: '请输入物资编码', trigger: 'blur' }
    ],
    DESCRIPTION: [
        { required: true, message: '请输入描述', trigger: 'blur' }
    ],
    QUNIT: [
        { required: true, message: '请输入单位', trigger: 'blur' }
    ],
    LB_TYPE: [
        { required: true, message: '请输入类型', trigger: 'blur' }
    ],
    // LB_SEX: [
    //     { required: true, message: '请输入性别', trigger: 'blur' }
    // ],
    // LB_HEIGHT: [
    //     { required: true, message: '请输入身高', trigger: 'blur' }
    // ],
    // LB_BUST: [
    //     { required: true, message: '请输入胸围', trigger: 'blur' }
    // ],
    // LB_SIZE: [
    //     { required: true, message: '请输入尺码', trigger: 'blur' }
    // ],
    // LB_STEREOTYPE: [
    //     { required: true, message: '请输入特征', trigger: 'blur' }
    // ],
})

//重置表单
const resetForm = () => {
    if (submitType.value === 'update' || submitType.value === 'copy') {
        editForm = objCopy(oldLbValue, editForm)
    } else if (submitType.value === 'add') {
        editForm = reactive({
            LBNUM: "",
            DESCRIPTION: "",
            QUNIT: "",
            LB_TYPE: "",
            LB_SEX: "",
            LB_HEIGHT: "",
            LB_BUST: "",
            LB_SIZE: "",
            LB_STEREOTYPE: ""
        })
    }
}

/**
 * 处理单个设备编辑、删除
 * @param type 操作类型，add：新增，update：编辑，del：删除
 * @param row 
 */
const editDatas = (type, row) => {
    // console.log(type)
    // console.log(row)
    submitType.value = type
    if (type === 'update') {
        drawerRef.value.open()
        title.value = "编辑"
        editForm = row
        //存储未修改前用户信息
        oldLbValue = objCopy(row, oldLbValue)
    }
    if (type === 'add') {
        drawerRef.value.open()
        title.value = "新增"
        resetForm()
    }
    if (type === "copy") {
        drawerRef.value.open()
        title.value = "复制"
        editForm = objCopy(row, editForm)
        editForm.LBNUM = ""
        editForm.DESCRIPTION += "_副本"
    }
}
//#endregion********************************************************************************************************




//#region 批量处理***************************************************************************************************
let batchIsShow = ref(false) //展示批量上传、批量删除状态
let selectShow = ref(false) //展示批量勾选状态
let selectRows = ref([]) //选中的设备行数据
let dialogRef = ref(null) //组件ref
let uploadKeys = ref([
    "LBNUM",
    "DESCRIPTION",
    "QUNIT",
    "LB_TYPE",
    "LB_SEX",
    "LB_HEIGHT",
    "LB_BUST",
    "LB_SIZE",
    "LB_STEREOTYPE",
]) //上传的字段

const batchEdit = () => {
    if (newDatasArr.value.length) selectShow.value = !selectShow.value
    batchIsShow.value = !batchIsShow.value
}

/**
 * 表格选中行数据变化时触发的方法
 * @param val 选中的设备行数据
 */
const handleSelectionChange = (val) => {
    selectRows.value = val
}

//批量上传
let uploadTable = ref([])
const batchUpload = () => {
    // console.log('batchUpload');
    dialogRef.value.open()
}

//关闭
const closeDialog = () => {
    uploadTable = ref([])
    dialogRef.value.close()
}

//接收子组件传递的excel数据，渲染至界面
let dialogTableRef = ref(null)
const uploadFile = (e) => {
    // console.log(e.value)            
    uploadTable = e.value
}

//下载
let downloadBoxShow = ref(false) //展示下载框状态
const showDownloadBox = () => {
    downloadBoxShow.value = !downloadBoxShow.value
}
/** 
 * 下载当前页
 * @param {Array} data 导出的数据数组对象
 * @param {String} tableId table标签的ID
 * @param {String} fileName 需要命名的文件名
 */
const downloadExcelPaging = (data, tableId, fileName) => {
    data.length ? exportClickFromTableId(tableId, fileName) : msgOpen("无数据可供下载，请先查询数据")
}
/**
 * 下载全部
 * @param {Array} data 导出的数据数组对象(未进行分页)
 * @param {String} fileName 需要命名的文件名
 */
const downloadExcelAll = (data, fileName) => {
    if (data.length) {
        let newData = JSON.parse(JSON.stringify(data)) //深拷贝      
        exportClickFromDatas(newData, fileName)
    } else {
        msgOpen("无数据可供下载，请先查询数据")
    }
}

/**
 * 导入模版下载
 * @param tableId table标签的ID
 * @param fileName 需要命名的文件名
 */
const downloadTemplate = (tableId, fileName) => {
    // console.log(tableId)
    exportClickFromTableId(tableId, fileName)
}
//********************************************************************************************************************** */
//#endregion


/**
 * 处理值编辑、删除、批量操作
 * @param type 操作类型，batchDel：批量删除
 * @param data 选中的值数据
 * 
 */
const editSubmit = async (type, data) => {
    // console.log(type)
    // console.log(data)
    if(type === 'add' || type === 'update' || type === 'copy'){
        updateFormRef.value.validate(async (valid) => {
            if(!valid){
                return false
            }else{
                let res = await editLbListInfo(type,data)
                console.log(res)
                drawerRef.value.hideLoading()
                drawerRef.value.close()
                res.status ? msgOpen(res.msg,'success') : msgOpen(res.msg)
                getDatas(paramsForm)
                resetForm() 
            }
        })       
    }
    if(type === 'delete'){
        let res = await confirm(data.LBNUM)
        if(res){
            let res = await editLbListInfo(type,data)
            res.status ? msgOpen(res.msg,'success') : msgOpen(res.msg)
            getDatas(paramsForm)
        }
    }
    if(type === 'batchAdd' || type === 'batchUpdate'){
        let rowsArr = ref([]) 
        let editBatchStatus = ref(true)
        for(let i = 0;i < data.length;i++){
            console.log(data[i]);    
            if(!data[i].LBNUM){
                msgOpen("请填写LBNUM！")
                editBatchStatus.value = false
                break
            }        
            /*if(type === 'batchUpdate' && !data[i].LBNUM){
                msgOpen("更新值，请填写LBNUM！")
                editBatchStatus.value = false
                break
            }else if(type === 'batchAdd' && data[i].LBNUM){
                msgOpen("新增值，LBNUM请留空！")
                editBatchStatus.value = false
                break  
            }*/else{
                rowsArr.value.push(data[i])
            }
        } 
        if(editBatchStatus.value){
            let res = await editManyLbListInfo(type,rowsArr.value)
            // console.log(res)            
            dialogRef.value.close()
            uploadTable = ref([])
            if(!res.status){
                msgOpen(res.msg)
            }else{
                res.resDatas.result.rowsAffected ? msgOpen(`${res.msg},共${res.resDatas.result.rowsAffected}条`,"success") 
                                                            : msgOpen(res.msg)
            }    
            getDatas(paramsForm) 
        }
    } 
    if(type === 'batchDel'){
        if(!data.length){
            msgOpen('未进行勾选，请勾选！')
        }else{
            let isDel = await confirmBatch(data.length) 
            if(isDel){
                let vNumArr = []
                data?.forEach(item => {
                    let arr = []
                    arr.push(item.LBNUM) 
                    vNumArr.push(arr)
                })
                let res = await editManyLbListInfo(type,vNumArr) 
                if(!res.status){
                    msgOpen(res.msg)
                }else{
                    res.resDatas.result.rowsAffected ? msgOpen(`${res.msg},共${res.resDatas.result.rowsAffected}条`,"success") 
                                                                : msgOpen(res.msg)
                }        
                getDatas(paramsForm) 
            }
        }
    }  
}
//#endregion

</script>

<style lang="less" scoped>
.params-form {
    height: 100%;
    width: 100%;

    .el-form {
        height: 100%;
        width: 100%;

        .one-box,
        .two-box {
            height: 50%;
            width: 100%;
            display: flex;
            justify-content: flex-start;
            align-items: center;

            .el-form-item {
                margin-bottom: 0;
                margin-right: .3125rem;
            }
        }
    }
}

.el-table {
    height: 95%;
}
</style>